package com.ssyc.gsk_tk.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.manager.PopUpManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.common.view.MyListView;
import com.ssyc.gsk_tk.R;
import com.ssyc.gsk_tk.adapter.LvSelfAnswerAdapter;
import com.ssyc.gsk_tk.adapter.RvWordFormAdapter;
import com.ssyc.gsk_tk.bean.BlankInfo;
import com.ssyc.gsk_tk.bean.InputWordInfo;
import com.ssyc.gsk_tk.bean.OverQuestion;
import com.ssyc.gsk_tk.bean.QuestionInfo;
import com.ssyc.gsk_tk.bean.TkInputInfo;
import com.ssyc.gsk_tk.util.TkKeys;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 用单词的适当形式填空
 * Created by Administrator on 2018/9/3 0003.
 */

public class TkWordFormInBlankFragment extends LazyBaseFragment {

    //页面对象
    private QuestionInfo.ListBean.ItemBean info;

    //已完成的页面对象
    private OverQuestion.ListBean.ItemBean overinfo;

    //页面的json数据
    private String jsonData;

    //页面对象
    private BlankInfo blankInfo;

    //标题
    private TextView tvTitle;

    //题目内容
    private TextView tvContent;

    //输入单词的列表
    private MyListView lv;
    private RvWordFormAdapter adapter;


    //已完成跳转进入需要展示的布局部分
    private LinearLayout ll_show_answer;
    //展示你选择的答案的ListView
    private MyListView lv_own_answer;
    //展示正确答案的ListView
    private MyListView lv_right_answer;
    //正确率
    private TextView tv_answer_present;
    //正确率的列表
    private MyListView lv_parse_answer;
    //解析
    private TextView tv_parse_answer;

    //已完成状态下自己作答的全部单词的集合
    private String[] ownAnswers;
    //以完成状态下的解析
    private String explain;

    //所有小题选项的正确率
    private List<OverQuestion.ListBean.ItemBean.ProbabilityBean> probability;


    public static final TkWordFormInBlankFragment newInstance(QuestionInfo.ListBean.ItemBean item, OverQuestion.ListBean.ItemBean overData) {
        TkWordFormInBlankFragment tkWordFormInBlankFragment = new TkWordFormInBlankFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("data", item);
        bundle.putSerializable("overdata", overData);
        tkWordFormInBlankFragment.setArguments(bundle);
        return tkWordFormInBlankFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_word_form_in_blank;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
    }

    @Override
    protected void init(View view, Bundle savedInstanceState) {
        super.init(view, savedInstanceState);
        //显示内容
        showContent();

        //获取页面对象
        initIntent();

        //初始化视图
        initView(view);

        //初始化底部的列表数据
        initRv();
    }


    /**
     * 获取页面对象
     */
    private void initIntent() {
        info = (QuestionInfo.ListBean.ItemBean) getArguments().getSerializable("data");

        //已完成的数据对象
        overinfo = (OverQuestion.ListBean.ItemBean) getArguments().getSerializable("overdata");

        if (info != null) {
            //获取页面的JSON数据
            jsonData = info.getContent();
            if (!TextUtils.isEmpty(jsonData)) {
                blankInfo = GsonUtil.jsonToBean(jsonData, BlankInfo.class);
            }
        } else if (overinfo != null) {
            //如果是已完成则解析overinfo
            jsonData = overinfo.getContent();
            if (!TextUtils.isEmpty(jsonData)) {
                blankInfo = GsonUtil.jsonToBean(jsonData, BlankInfo.class);

                //获取自己作答的所有内容
                ownAnswers = overinfo.getOption_content().split("￠");

                //获取解析,注意:这里的解析只有一个
                explain = overinfo.getAnalysis();

                //获取所有小题的争取率
                probability = overinfo.getProbability();

            }
        }
    }

    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        tvTitle = view.findViewById(R.id.tv_title);
        tvContent = view.findViewById(R.id.tv_content);
        tvContent.setText(blankInfo.getContent());
        lv = view.findViewById(R.id.lv);
        //已完成跳转进入需要展示的布局部分
        ll_show_answer = view.findViewById(R.id.ll_show_answer);
        lv_own_answer = view.findViewById(R.id.lv_own_answer);
        lv_right_answer = view.findViewById(R.id.lv_right_answer);
        tv_answer_present = view.findViewById(R.id.tv_answer_present);
        lv_parse_answer = view.findViewById(R.id.lv_parse_answer);
        tv_parse_answer = view.findViewById(R.id.tv_parse_answer);

        if (overinfo != null) {
            //已完成
            lv.setEnabled(false);
            ll_show_answer.setVisibility(View.VISIBLE);
            //设置已完成状态下用户的数据
            initHasCompetedData();
        } else {
            //未完成
            lv.setEnabled(true);
            ll_show_answer.setVisibility(View.GONE);
        }
    }


    /**
     * 初始化列表数据
     */
    private void initRv() {
        //创建数据集合
        List<InputWordInfo> oldDatas = new ArrayList<>();
        List<String> rightAnswers = blankInfo.getList();
        for (int i = 0; i < rightAnswers.size(); i++) {
            InputWordInfo info = new InputWordInfo();
            info.flag = (1 + i) + "";
            info.rightAnswer = rightAnswers.get(i);
            if (overinfo != null) {
                // 已完成状态
                info.word = ownAnswers[i];
            }
            oldDatas.add(info);
        }

        adapter = new RvWordFormAdapter(getContext(), oldDatas, R.layout.tk_rv_item_form_blank);
        lv.setAdapter(adapter);

        //点击事件弹框输入内容
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                PopUpManager.showPop(getContext(), R.layout.tk_pop_input_form_blank, 0.3f, getActivity(), new PopUpManager.onGetViewListener() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
                        TextView tv_ok = view.findViewById(R.id.tv_ok);
                        EditText et_input = view.findViewById(R.id.et_input);
                        TextView tv_content = view.findViewById(R.id.tv_content);

                        tv_cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                PopUpManager.dismiss();
                            }
                        });

                        tv_ok.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //获取输入的内容
                                String content = et_input.getText().toString().trim();
                                if (TextUtils.isEmpty(content)) {
                                    UiUtils.Toast("您还未输入单词", false);
                                    return;
                                } else {
                                    PopUpManager.dismiss();
                                    oldDatas.get(pos).word = content;
                                    adapter.notifyDataSetChanged();
                                    //将输入的答案通过EventBus回传TkMainActivity进行更新
                                    //回传当前提醒的TypeId,用单词的适当形式填空题型对应的是7
                                    int typeId = 7;
                                    //currPos对应的每个大题的小题的位置
                                    int choosePos = pos;
                                    //用户选的答案的索引对应ABCDEF
                                    String chooseAnswer = content;
                                    //是否如正确
                                    boolean chooseResult = false;
                                    if (chooseAnswer.equals(oldDatas.get(pos).rightAnswer)) {
                                        //用户作答正确
                                        chooseResult = true;
                                    } else {
                                        //用户作答错误
                                        chooseResult = false;
                                    }
                                    BusInfo busInfo = new BusInfo();
                                    busInfo.tkMsg = TkKeys.TKMsg;
                                    busInfo.tkChoosePos = choosePos;
                                    busInfo.tkChooseAnswer = chooseAnswer;
                                    busInfo.tkChooseResult = chooseResult;
                                    busInfo.typeId = typeId;
                                    EventBus.getDefault().post(busInfo);
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    /**
     * 初始化你的作答,正确答案,答案正确率和解析
     * 需要通过用户是否是已完成和未完成的状态来确定该部分的视图显示与隐藏,同时设置数据的数据源不一样
     * 该设置需要取已完成状态的数据源集合
     */
    private void initHasCompetedData() {

        //创建你的作答的数据
        List<TkInputInfo> selfDatas = new ArrayList<>();
        for (int i = 0; i < ownAnswers.length; i++) {
            TkInputInfo info = new TkInputInfo();
            info.index = "(" + (i + 1) + ")";
            info.content = ownAnswers[i];
            selfDatas.add(info);
        }

        //自己的答案选项
        LvSelfAnswerAdapter lvSelfAdapter = new LvSelfAnswerAdapter(getContext(), selfDatas, R.layout.tk_lv_answer);
        lv_own_answer.setAdapter(lvSelfAdapter);

        //模拟正确答案的数据
        List<String> rightsAnswers = blankInfo.getList();
        List<TkInputInfo> rightDatas = new ArrayList<>();
        for (int i = 0; i < rightsAnswers.size(); i++) {
            TkInputInfo info = new TkInputInfo();
            info.index = "(" + (i + 1) + ")";
            info.content = rightsAnswers.get(i);
            rightDatas.add(info);
        }

        LvSelfAnswerAdapter lvRightAdapter = new LvSelfAnswerAdapter(getContext(), rightDatas, R.layout.tk_lv_answer);
        lv_right_answer.setAdapter(lvRightAdapter);

        //设置正确率
        if (overinfo != null) {
            //设置正确率的列表
            if(!TextUtils.isEmpty(overinfo.getBig_probability())){
                tv_answer_present.setText(overinfo.getBig_probability());
            }else{
                tv_answer_present.setText("100.0%");
            }

            //创建解析的数据集合
            List<TkInputInfo> parseAnswerDatas = new ArrayList<>();
            for (int i = 0; i < rightsAnswers.size(); i++   ) {
                TkInputInfo info = new TkInputInfo();
                info.index = "(" + (i + 1) + ")";

                //这里Content理解为该题的正确率
                if (probability != null && probability.size() != 0) {

//                    if(TextUtils.isEmpty(probability.get(i).getZ())){
//                        info.content = "100.0%";
//                    }else{
//                        info.content = probability.get(i).getZ();
//                    }
                    if(TextUtils.isEmpty(probability.get(i).getpro())){
                        info.content = "100.0%";
                    }else{
                        info.content = probability.get(i).getpro();
                    }
                } else {
                    info.content = "100.0%";
                }

                parseAnswerDatas.add(info);
            }
            LvSelfAnswerAdapter lvParseAnswerAdapter = new LvSelfAnswerAdapter(getContext(), parseAnswerDatas, R.layout.tk_lv_parse_answer);
            lv_parse_answer.setAdapter(lvParseAnswerAdapter);
        }

        //设置解析
        tv_parse_answer.setText(explain);
    }
}
